package fr.curriculumvitae.anthony.stephan.fragments;

import fr.curriculumvitae.anthony.stephan.R;
import fr.curriculumvitae.anthony.stephan.SwipeyTabsActivity;
import fr.curriculumvitae.anthony.stephan.pinnedsections.PinnedSectionListView;
import fr.curriculumvitae.anthony.stephan.pinnedsections.PinnedSectionListView.PinnedSectionListAdapter;
import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class CenterOfInterestsFragment extends ListFragment implements OnClickListener {
	
	private static Fragment INSTANCE = null;
	
	private static SwipeyTabsActivity fragmentController;
	
	private static final int SECTION_RECREATIONS 		= 0;
	private static final int SECTION_TECHNICS 			= 1;
	private static final int SECTION_CULTURE			= 2;
	private static final int SECTION_READING			= 3;
	
	private static String[] sectionLabels;
	private static String[] recreationsLabels;
	private static String[] technicsLabels;
	private static String[] cultureLabels;
	private static String[] readingsLabels;
	private static int[] recreationsRates;
	private static int[] technicsRates;
	private static int[] cultureRates;
	private static int[] readingsRates;
	
	public static Fragment getInstance() {
		if(INSTANCE == null) {
			INSTANCE = new CenterOfInterestsFragment();
		} 
		return INSTANCE;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.pinned_head_list, null);
		return root;
	}
	
 	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
 		fragmentController = (SwipeyTabsActivity) getActivity();
 		inflateArrays();
		initializeAdapter();
		initializeListView();
	}
 	
	private void initializeListView() {
	    getListView().setPadding(0, 0, 0, 0);
	    ((PinnedSectionListView)getListView()).setShadowVisible(false);
	}

    private void inflateArrays() {
    	sectionLabels = getResources().getStringArray(R.array.interests_sections_labels);
    	recreationsLabels = getResources().getStringArray(R.array.interests_recreation_labels);
    	technicsLabels = getResources().getStringArray(R.array.interests_technics_labels);
    	cultureLabels = getResources().getStringArray(R.array.interests_culture_labels);
    	readingsLabels = getResources().getStringArray(R.array.interests_readings_labels);
    	recreationsRates = getResources().getIntArray(R.array.interests_recreation_rates);
    	technicsRates = getResources().getIntArray(R.array.interests_technics_rates);
    	cultureRates = getResources().getIntArray(R.array.interests_culture_rates);
    	readingsRates = getResources().getIntArray(R.array.interests_readings_rates);
    }

    private void initializeAdapter() {
    	setListAdapter(new SkillsAdapter(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1));
    }
    
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
	    Interest item = (Interest) getListView().getAdapter().getItem(position);
	    if (item != null && v.getTag() != null) {
	    	if(SwipeyTabsActivity.mToast != null) {
	    		SwipeyTabsActivity.mToast.cancel();
	    	}
	    	SwipeyTabsActivity.mToast = Toast.makeText(getActivity(), v.getTag().toString(), Toast.LENGTH_SHORT);
	    	SwipeyTabsActivity.mToast.show();
	    }
	}

    @Override
    public void onClick(View v) {
    	/* nothing to do */
    }
    
    
      /////////////
     // ADAPTER //
    /////////////
    
	private static class SkillsAdapter extends ArrayAdapter<Interest> implements PinnedSectionListAdapter {

        public SkillsAdapter(Context context, int resource, int textViewResourceId) {
            super(context, resource, textViewResourceId);

            final int sectionsNumber = sectionLabels.length;
            prepareSections(sectionsNumber);
            
            int sectionPosition = 0, listPosition = 0;
            
            for(char i=0; i<sectionsNumber; i++) {
                Interest section = new Interest(Interest.SECTION, sectionLabels[i], 0);
                section.sectionNumber = i;
                section.sectionPosition = sectionPosition;
                section.listPosition = listPosition++;
                onSectionAdded(section, sectionPosition);
                add(section);
                
                Interest item;
                final int skillsNumber;
                switch(i) {
                	case SECTION_RECREATIONS:
                		skillsNumber = recreationsLabels.length;
                		for(int j=0; j<skillsNumber; j++) {
	                        item = new Interest(Interest.ITEM, recreationsLabels[j], recreationsRates[j]);
	                        item.sectionNumber = i;
	                        item.sectionPosition = sectionPosition;
	                        item.listPosition = listPosition++;
	                        add(item);
                		}
                		break;
                	case SECTION_TECHNICS:
                		skillsNumber = technicsLabels.length;
                		for(int j=0; j<skillsNumber; j++) {
	                        item = new Interest(Interest.ITEM, technicsLabels[j], technicsRates[j]);
	                        item.sectionNumber = i;
	                        item.sectionPosition = sectionPosition;
	                        item.listPosition = listPosition++;
	                        add(item);
                		}
                		break;
                	case SECTION_CULTURE:
                		skillsNumber = cultureLabels.length;
                		for(int j=0; j<skillsNumber; j++) {
	                        item = new Interest(Interest.ITEM, cultureLabels[j], cultureRates[j]);
	                        item.sectionNumber = i;
	                        item.sectionPosition = sectionPosition;
	                        item.listPosition = listPosition++;
	                        add(item);
                		}
                		break;
                	case SECTION_READING:
                		skillsNumber = readingsLabels.length;
                		for(int j=0; j<skillsNumber; j++) {
	                        item = new Interest(Interest.ITEM, readingsLabels[j], readingsRates[j]);
	                        item.sectionNumber = i;
	                        item.sectionPosition = sectionPosition;
	                        item.listPosition = listPosition++;
	                        add(item);
                		}
                		break;
                }
                sectionPosition++;
            }
        }

        protected void prepareSections(int sectionsNumber) {
        	/* nothing to do */
        }
        
        protected void onSectionAdded(Interest section, int sectionPosition) { 
        	/* nothing to do */
        }

        @Override 
        public View getView(int position, View convertView, ViewGroup parent) {
        	LayoutInflater inflater = (LayoutInflater) fragmentController.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	View view = null;
        	Interest item = getItem(position);
        	
        	if(item.type == Interest.SECTION) {
        		view = inflater.inflate(R.layout.layout_header_with_icon, null);
                ImageView ivSkillIcon = (ImageView) view.findViewById(R.id.imageView_skill_header_icon);
                TextView tvSkillLavel = (TextView) view.findViewById(R.id.textView_skill_header_label);
                tvSkillLavel.setText(item.label);
                switch(item.sectionNumber) {
	                case SECTION_RECREATIONS:
	                	ivSkillIcon.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.ic_recreations));
	                	view.setBackgroundColor(fragmentController.getResources().getColor(R.color.pinnedheader_head_green));
	                	break;
	                case SECTION_TECHNICS:
	                	ivSkillIcon.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.ic_technics));
	                	view.setBackgroundColor(fragmentController.getResources().getColor(R.color.pinnedheader_head_blue));
	                	break;
	                case SECTION_CULTURE:
	                	ivSkillIcon.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.ic_culture));
	                	view.setBackgroundColor(fragmentController.getResources().getColor(R.color.pinnedheader_head_violet));
	                	break;
	                case SECTION_READING:
	                	ivSkillIcon.setImageDrawable(fragmentController.getResources().getDrawable(R.drawable.ic_readings));
	                	view.setBackgroundColor(fragmentController.getResources().getColor(R.color.pinnedheader_head_red));
	                	break;
                }
        		view.setTag(null);
        		
        	} else if(item.type == Interest.ITEM) {
        		view = inflater.inflate(R.layout.layout_row_interest, null);
        		TextView tvSkillLabel = (TextView) view.findViewById(R.id.textView_interest_label);
        		ProgressBar pbInterestRate = (ProgressBar) view.findViewById(R.id.textView_interest_rate);
        		tvSkillLabel.setText(item.label);
        		Rect bounds = pbInterestRate.getProgressDrawable().getBounds();
                switch(item.sectionNumber) {
	                case SECTION_RECREATIONS:
	                	pbInterestRate.setProgressDrawable(fragmentController.getResources().getDrawable(R.drawable.custom_progress_bar_green));
	                	break;
	                case SECTION_TECHNICS:
	                	pbInterestRate.setProgressDrawable(fragmentController.getResources().getDrawable(R.drawable.custom_progress_bar_blue));
	                	break;
	                case SECTION_CULTURE:
	                	pbInterestRate.setProgressDrawable(fragmentController.getResources().getDrawable(R.drawable.custom_progress_bar_violet));
	                	break;
	                case SECTION_READING:
	                	pbInterestRate.setProgressDrawable(fragmentController.getResources().getDrawable(R.drawable.custom_progress_bar_red));
	                	break;
                }
                pbInterestRate.getProgressDrawable().setBounds(bounds);
                pbInterestRate.setProgress(1);
                pbInterestRate.setMax(100);
                pbInterestRate.setProgress(item.rate);
        		view.setTag(item.label+" : "+item.rate+"%");
        	}
        	
            return view;
        }

        @Override 
        public int getViewTypeCount() {
            return 2;
        }

        @Override 
        public int getItemViewType(int position) {
            return getItem(position).type;
        }

        @Override
        public boolean isItemViewTypePinned(int viewType) {
            return viewType == Interest.SECTION;
        }
    }

	@SuppressWarnings("unused")
	private static class Interest {
		public static final int ITEM = 0;
		public static final int SECTION = 1;
		public int sectionNumber;
		public int sectionPosition;
		public int listPosition;
		public final int type;
		public final String label;
		public final int rate;
		
		public Interest(int type, String label, int rate) {
		    this.type = type;
		    this.label = label;
		    this.rate = rate;
		}
		@Override 
		public String toString() {
			return label;
		}
	}

}
